package cn.attackme.myuploader.result;

import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @Author:arshost
 * @Description:
 * @Date: Create in 10:54 2019-12-31
 * @Modify:
 */
@Data
public class ResultJson <T> implements Serializable {

    private Boolean success;
    private Integer code;
    private String msg;
    private T data;

    public ResultJson (){

    }

    public ResultJson (String msg) {
        this.msg = msg;
    }

    public ResultJson(boolean success) {
        this.success = success;
        this.code = success ? ResponseStatues.SUCCESS.getCode() : ResponseStatues.FAIL.getCode();
        this.msg = success ? ResponseStatues.SUCCESS.getMessage() : ResponseStatues.FAIL.getMessage();
    }

    public ResultJson(boolean success, ResponseStatues responseStatues) {
        this.success = success;
        this.code = success ? ResponseStatues.SUCCESS.getCode() : (responseStatues == null ? ResponseStatues.FAIL.getCode() : responseStatues.getCode());
        this.msg = success ? ResponseStatues.SUCCESS.getMessage() : (responseStatues == null ? ResponseStatues.FAIL.getMessage() : responseStatues.getMessage());
    }

    public ResultJson (boolean success, T data) {
        this.success = success;
        this.code = success ? ResponseStatues.SUCCESS.getCode() : ResponseStatues.FAIL.getCode();
        this.msg = success ? ResponseStatues.SUCCESS.getMessage() : ResponseStatues.FAIL.getMessage();
        this.data = data;
    }

    public ResultJson(boolean success, ResponseStatues responseStatues, T data) {
        this.success = success;
        this.code = success ? ResponseStatues.SUCCESS.getCode() : (responseStatues == null ? ResponseStatues.FAIL.getCode() : responseStatues.getCode());
        this.msg = success ? ResponseStatues.SUCCESS.getMessage() : (responseStatues == null ? ResponseStatues.FAIL.getMessage() : responseStatues.getMessage());
        this.data = data;
    }

    public static ResultJson success(String msg) {
        return new ResultJson(msg);
    }

    public Object error(HttpStatus httpStatus, String msg) {
        this.code = httpStatus.value();
        this.msg = msg;
        return this;
    }
}
